package gat.common.util;


import java.util.Collection;
import java.util.Objects;
import java.util.Optional;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.bitsun.core.common.exception.AppException;

import gat.common.exception.ExceptionEnum;


public class ConditionUtil {

    public static void mustNotNull(Object o, String msg) {
        must(Objects.nonNull(o), msg);
    }

    public static void mustNotNull(Object o, ExceptionEnum e) {
        must(Objects.nonNull(o), e);
    }

    public static void mustNotEmpty(Collection<?> c, String msg) {
        must(CollectionUtils.isNotEmpty(c), msg);
    }

    public static void mustNotEmpty(Collection<?> c, ExceptionEnum e) {
        must(CollectionUtils.isNotEmpty(c), e);
    }

    public static void mustNotBlank(String s, String msg) {
        must(StringUtils.isNotBlank(s), msg);
    }

    public static void mustNotBlank(String s, ExceptionEnum e) {
        must(StringUtils.isNotBlank(s), e);
    }

    public static void mustEquals(Object a, Object b, String msg) {
        must(Objects.equals(a, b), msg);
    }

    public static void mustEquals(Object a, Object b, ExceptionEnum e) {
        must(Objects.equals(a, b), e);
    }

    public static void mustNotEquals(Object a, Object b, String msg) {
        must(!Objects.equals(a, b), msg);
    }

    public static void mustNotEquals(Object a, Object b, ExceptionEnum e) {
        must(!Objects.equals(a, b), e);
    }

    public static void must(Boolean b, String msg) {
        if (!Optional.ofNullable(b).orElse(false)) {
            ExceptionUtil.throwNew(ExceptionEnum.EXCEPTION_ENUM_10001, msg);
        }
    }

    public static void must(Boolean b, ExceptionEnum e) {
        if (!Optional.ofNullable(b).orElse(false)) {
            ExceptionUtil.throwNew(e);
        }
    }

    public static void notNull(Object o, String message) {
        test(o == null ,message);
    }

    public static void test(boolean flag, String message) {
        if (flag) {
            throw  new AppException(message);
        }
    }

    public static void mustV2(Boolean b, String msg) {
        if (!Optional.ofNullable(b).orElse(false)) {
            ExceptionUtil.throwNew(ExceptionEnum.EXCEPTION_ENUM_99998, msg);
        }
    }
}
